package com.hong.pdf2img.utils;

import com.hong.pdf2img.MainApplication;
import javafx.stage.FileChooser;
import javafx.stage.Window;
import java.io.File;
import java.util.List;

public class FileDialogUtils {

    /**
     * 显示保存文本文件对话框
     */
    public static File showSaveTextDialog(Window ownerWindow, String initialFileName,String filePath) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("保存文本文件");
        if (initialFileName != null) fileChooser.setInitialFileName(initialFileName);
        // 设置文件类型过滤器
        FileChooser.ExtensionFilter txtFilter = new FileChooser.ExtensionFilter("文本文件 (*.txt)", "*.txt");
        FileChooser.ExtensionFilter allFilter = new FileChooser.ExtensionFilter("所有文件 (*.*)", "*.*");
        fileChooser.getExtensionFilters().addAll(txtFilter, allFilter);
        if(filePath!=null && !filePath.isEmpty()) fileChooser.setInitialDirectory(new File(filePath));
        return fileChooser.showSaveDialog(ownerWindow);
    }

    /**
     * 显示保存图片对话框
     */
    public static File showSaveImageDialog(Window ownerWindow, String initialFileName) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("保存图片");
        if (initialFileName != null) fileChooser.setInitialFileName(initialFileName);
        // 图片格式过滤器
        FileChooser.ExtensionFilter pngFilter = new FileChooser.ExtensionFilter("PNG 图片 (*.png)", "*.png");
        FileChooser.ExtensionFilter jpgFilter = new FileChooser.ExtensionFilter("JPEG 图片 (*.jpg)", "*.jpg");
        FileChooser.ExtensionFilter allFilter = new FileChooser.ExtensionFilter("所有文件 (*.*)", "*.*");
        fileChooser.getExtensionFilters().addAll(pngFilter, jpgFilter, allFilter);
        return fileChooser.showSaveDialog(ownerWindow);
    }

    /**
     * 显示保存PDF对话框
     */
    public static File showSavePdfDialog(Window ownerWindow, String initialFileName) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("保存PDF文档");
        if (initialFileName != null) fileChooser.setInitialFileName(initialFileName);
        FileChooser.ExtensionFilter pdfFilter = new FileChooser.ExtensionFilter("PDF 文档 (*.pdf)", "*.pdf");
        FileChooser.ExtensionFilter allFilter = new FileChooser.ExtensionFilter("所有文件 (*.*)", "*.*");
        fileChooser.getExtensionFilters().addAll(pdfFilter, allFilter);
        return fileChooser.showSaveDialog(ownerWindow);
    }

    /**
     * 自定义保存对话框
     */
    public static File showCustomSaveDialog(Window ownerWindow, String title, String initialFileName, List<FileChooser.ExtensionFilter> filters) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle(title);
        if (initialFileName != null) fileChooser.setInitialFileName(initialFileName);
        if (filters != null && !filters.isEmpty()) fileChooser.getExtensionFilters().addAll(filters);
        return fileChooser.showSaveDialog(ownerWindow);
    }
}
